<?php
class Database extends mysqli
{
	/* This class will provide methods for accessing the MySQL database
	 * It is derived from PHP's built in mysqli class.
	 * It will be created as a super global each time a page is requested,
	 * so it is always available within methods of any of the other classes
	 */

	private $host = 'localhost',
		$username = 'juliet',
		$password = 'password',
		$table	  = 'table';

	public function __construct(){
		parent::__construct($host,$username,$password,$table);
		//create a new connection
	}
	
	public function getAccount($AccountID){
		//gets Account info for this ID
		$sql = "SELECT * FROM tblAccount WHERE AccountID=" . $AccountID;
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result, MYSQL_ASSOC));		
		$hotspot = array("AccountID" => $AccountID, "MediaId" => $row['MediaID'], "Username" => $row['Username'], "Password" => $row['Password']);
		return $account;	
	}	
	
	public function getAccounts(){
		//gets list of Social Media Account IDs
		$sql = "SELECT AccountId FROM tblAccount";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result, MYSQL_ASSOC));
		$accounts = $row;
		return $accounts	
	}
	
	public function newAccounts($args){
		//create a new hotspot from $args
		$sql = "INSERT INTO tblAccounts (MediaID, Username, Password) VALUES('" . $args['MediaID'] . "', '" . $args['Username'] . "', '" . $args['Password'] . "')";
		mysql_query($sql);
		//adds an entry in the account-hotspot link table for each hotspot
		$hotspots = $this->getHotspots();
		$accountid = mysql_insert_id();
		foreach($hotspots as $hotspot)
		{
			$sql = "INSERT INTO tblAccountHotspot (AccountID, HotspotID) VALUES('" . $accountid . "', '" . $hotspot . "')";
			mysql_query($sql);
		}
	}	
	
	public function newHotspot($args){
		//create a new hotspot from $args and returns ID
		$sql = "INSERT INTO tblHotspot (ActionID, x1, y1, x2, y2) VALUES('" . $args['ActionID'] . "', '" . $args['x1'] . "', '" . $args['y1'] . "', '" . $args['x2'] . "', '" . $args['y2'] . "')";
		mysql_query($sql);
		//adds an entry in the account-hotspot link table for each account
		$accounts = $this->getAccounts();
		$hotspotid = mysql_insert_id();
		foreach($accounts as $account)
		{
			$sql = "INSERT INTO tblAccountHotspot (AccountID, HotspotID) VALUES('" . $account . "', '" . $hotspotid . "')";
			mysql_query($sql);
		}
		return $hotspotid;
	}
	
	public function setHospot($args){
		//create a new hotspot from $args and returns ID
		$sql = "REPLACE INTO tblHotspot (ActionID, x1, y1, x2, y2) VALUES('" . $args['ActionID'] . "', '" . $args['x1'] . "', '" . $args['y1'] . "', '" . $args['x2'] . "', '" . $args['y2'] . "')";
		mysql_query($sql);
		return $hotspotid;
	}

	public function getHotspots(){
		//gets list of Hotspot IDs
		$sql = "SELECT HotspotID FROM tblHotspot";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result, MYSQL_ASSOC));
		$hotspots = $row;
		return $hotspots	
	}
	public function getHotspot($HotspotID){
		//gets Hotspot info for this ID
		$sql = "SELECT * FROM tblHotspot WHERE HotspotID=" . $HotspotID;
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result, MYSQL_ASSOC));		
		$hotspot = array("HotspotID" => $HotspotID, "ActionID" => $row['ActionID'], "ActionDesc" => "", "x1" => $row['x1'], "y1" => $row['y1'], "x2" => $row['x2'], "y2" => $row['y2']);
		$sql = "SELECT Desc FROM tblAction WHERE ActionID=" . $row['ActionID'];
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result, MYSQL_ASSOC));
		$hotspot['ActionDesc'] = $row['Desc'];
		return $hotspot;
	}
	public function getStats(){
		//gets stats for this user
	}
}